package com.tchepannou.limbe.gae.converter;

import com.google.appengine.api.datastore.Link;
import com.google.appengine.api.datastore.Text;
import com.tchepannou.util.converter.Converter;

import java.net.URI;
import java.net.URL;

/**
 * User: herve
 * Date: 13-06-30 6:13 AM
 */
public class LinkConverter
    implements Converter
{
    //-- Converter overrides
    @Override
    public Object convert(Object value)
    {
        if (value == null)
        {
            return null;
        }
        else if (value instanceof Link)
        {
            return value;
        }
        else if (value instanceof String)
        {
            return new Link((String)value);
        }
        else if (value instanceof Text)
        {
            return new Link(((Text)value).getValue ());
        }
        else if (value instanceof URL || value instanceof URI)
        {
           return new Link(value.toString());
        }
        else
        {
            return null;
        }
    }
}
